package cn.iocoder.yudao.module.wine.service.uservisitor;

import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.wine.controller.app.userfans.vo.AppUserFansReqVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.wine.controller.app.uservisitor.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.uservisitor.UserVisitorDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.wine.dal.mysql.uservisitor.UserVisitorMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wine.enums.ErrorCodeConstants.*;

/**
 * 访客信息 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class UserVisitorServiceImpl implements UserVisitorService {

    @Resource
    private UserVisitorMapper userVisitorMapper;

    @Override
    public Long createUserVisitor(AppUserVisitorSaveReqVO createReqVO) {
        // 插入
        UserVisitorDO userVisitor = BeanUtils.toBean(createReqVO, UserVisitorDO.class);
        userVisitorMapper.insert(userVisitor);
        // 返回
        return userVisitor.getId();
    }

    @Override
    public void updateUserVisitor(AppUserVisitorSaveReqVO updateReqVO) {
        // 校验存在
        validateUserVisitorExists(updateReqVO.getId());
        // 更新
        UserVisitorDO updateObj = BeanUtils.toBean(updateReqVO, UserVisitorDO.class);
        userVisitorMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserVisitor(Long id) {
        // 校验存在
        validateUserVisitorExists(id);
        // 删除
        userVisitorMapper.deleteById(id);
    }

    private void validateUserVisitorExists(Long id) {
        if (userVisitorMapper.selectById(id) == null) {
            throw exception(INFO_NOT_EXISTS);
        }
    }

    @Override
    public UserVisitorDO getUserVisitor(Long id) {
        return userVisitorMapper.selectById(id);
    }

    @Override
    public PageResult<UserVisitorDO> getUserVisitorPage(AppUserVisitorPageReqVO pageReqVO) {
        return userVisitorMapper.selectPage(pageReqVO);
    }

    @Override
    public List<AppUserVisitorListRespVO> getUserVisitorList(AppUserFansReqVO reqVO) {
        if (reqVO.getUserId() != null) {
            return userVisitorMapper.getUserVisitorList(reqVO.getUserId());
        }
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        return userVisitorMapper.getUserVisitorList(userId);

    }

    @Override
    public void addUserVisitorRecord(UserVisitorDO userVisitorDO) {
        userVisitorMapper.insert(userVisitorDO);
    }

    @Override
    public Long getVisitorNum(Long id) {
        List<UserVisitorDO> list = userVisitorMapper.selectList(new MPJLambdaWrapperX<UserVisitorDO>()
                .eq(UserVisitorDO::getUserId, id)
                .groupBy(UserVisitorDO::getVisitorId));
        if (CollectionUtils.isEmpty(list)){
            return 0L;
        }
        return Long.valueOf(String.valueOf(list.size()));
    }

}
